package com.ibm.spatiotemp.server.datasources.ifx;

import java.util.Collection;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPoint;
import com.ibm.spatiotemp.server.datasources.DataSchema;
import com.ibm.spatiotemp.server.datasources.QueryStringFormatter;

public class InformixQueryStringFormatter extends QueryStringFormatter {
	public static Logger logger = Logger.getLogger(InformixQueryStringFormatter.class);
	// use virtual table
	// select t, x, y from vt_vehicle where modid='id1' and t>= starttime and t<= endtime
	public static String scanquery_format = "select %s, %s, %s from %s where %s=\'%s\' and %s>=%s and %s<=%s";
	// insert into vt_vehicle (modid,t,x,y) values ('1','2008-02-02 15:00:00',117.40061,39.907);
	// insert into vt_vehicle (modid,t,x,y) values (?, ?, ?, ?);
	public static String writedata_format = "insert into %s (%s,%s,%s,%s) values (%s)";
	
	@Override
	public String RawDataScanQueryString(DataSchema schema, String startts,
			String endts, String movobjid) {
		
		String querystring = String.format(
				scanquery_format,
				schema.getTs_colname(),
				schema.getX_colname(),
				schema.getY_colname(),
				schema.getTab_name(),
				schema.getMovobj_colname(),
				movobjid,
				schema.getTs_colname(),
				startts,
				schema.getTs_colname(),
				endts
				);
//		logger.info(querystring);
		return querystring;
	}

	@Override
	public String writeDataString(DataSchema schema,
			Collection<TSPoint> tspoints) {
		StringBuffer strbuf = new StringBuffer();
		if(tspoints.size()>=1){
			strbuf.append("?, ?, ?, ?");
			String querystring = String.format(writedata_format, 
					schema.getTab_name(),
					schema.getMovobj_colname(),
					schema.getTs_colname(),
					schema.getX_colname(),
					schema.getY_colname(),
					strbuf.toString()
					);
//			logger.info(querystring);
			return querystring;
		}
		else return "";
	}
	
}
